package net.trustie.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.commons.codec.digest.DigestUtils;

import net.trustie.utils.DateHandler;
import net.trustie.utils.StringHandler;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import core.AfterExtractor;
import core.Page;
import core.ValidateExtractor;

public class neitui_Model implements AfterExtractor, ValidateExtractor {
	@ExtractBy("//*[@id='detail']/div/ul/li/div[2]/div[1]/a/text()")
	private String author_name = "";
	@ExtractBy("//*[@id='detail']/div/ul/li/div[2]/div[1]/a/@href")
	private String author_url = "";
	private String post_url = "";
	@ExtractBy("//*[@id='detail']/div/ul/li/div[2]/div[1]/text()")
	private String post_time = "";
	@ExtractBy("//*[@id='detail']/div/ul/li/div[2]/div[2]/strong/text()")
	private String post_title = "";
	@ExtractBy("//*[@id='detail']/div/ul/li/div[2]/div[4]/ul/li/allText()")
	private List<String> tags;
	private String post_tags = "";
	@ExtractBy("//*[@id='detail']/div/ul/li/div[2]/div[6]/text()")
	private String post_content = "";
	@ExtractBy("//*[@id='detail']/div/ul/li/div[2]/div[2]/span[1]/text()")
	private String salary = "";
	@ExtractBy("//*[@id='detail']/div/ul/li/div[2]/div[2]/span[2]/text()")
	private String experience = "";
	@ExtractBy("//*[@id='neituiDetail']/div[3]/div[1]/div[1]/div[2]/a/text()")
	private String comp_name = "";
	@ExtractBy("//*[@id='neituiDetail']/div[3]/div[1]/div[1]/div[2]/a/@href")
	private String comp_url = "";
	@ExtractBy("//*[@id='detail']/div/ul/li/div[2]/div[3]/span[2]/text()")
	private String addr = "";
	@ExtractBy("//*[@id='neituiDetail']/div[3]/div[1]/dl[2]/dd[1]/text()")
	private String comp_scale = "";
	@ExtractBy("//*[@id='neituiDetail']/div[3]/div[1]/dl[2]/dd[2]/text()")
	private String comp_field = "";
	@ExtractBy("//*[@id='neituiDetail']/div[3]/div[1]/dl[2]/dd[3]/text()")
	private String financing = "";
	@ExtractBy("//*[@id='neituiDetail']/div[3]/div[1]/dl[3]/dd/text()")
	private String comp_vision = "";
	private String extractTime = "";
	private int history = 0;

	private String source ="neitui";
	private String type="job";
	private String url_md5;
	private String similar_position;
	
	@ExtractBy("//*[@id='sim_jobs']/div[2]/ul/li/a/@href")
	private List<String> similar_positions;
	public void afterProcess(Page page) {
		//处理addr
		this.similar_position = StringHandler.combineTags(this.similar_positions);
		this.addr=addr.substring(3, 5);
		// 处理page_url
		this.post_url = page.getPageUrl();
		this.url_md5 = DigestUtils.md5Hex(this.post_url);
		
		// 处理post_time
		String s1 = StringHandler.findRigthString(this.post_time, ") ", " 发布");
		String s2 = DateHandler.replaceChinesDate(s1);
		String pat = "MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(pat, Locale.US);
		Date date = null;
		try {
			date = sdf.parse(s2);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String pat2 = "2015-MM-dd HH:mm:ss";
		SimpleDateFormat fomater = new SimpleDateFormat(pat2);
		this.post_time = fomater.format(date);
		// 处理salary
		this.salary = StringHandler.findRigthString(salary, "[", "]");
		// 处理experience
		this.experience = StringHandler.findRigthString(experience, "[", "]");
		// 处理post_tags
		this.post_tags = StringHandler.combineTags(tags);
		// 处理extractTime
		this.extractTime = DateHandler.getExtractTime();
	}

	public void validate(Page page) {
		if (StringHandler.isAtLeastOneBlank(this.post_url, this.post_title,
				this.extractTime)) {
			page.setResultSkip(this, true);
			return;
		}
		if (!DateHandler.canFormatToDate(this.post_time, this.extractTime)) {
			page.setResultSkip(this, true);
		}	
	}

	public String getAuthor_name() {
		return author_name;
	}

	public void setAuthor_name(String author_name) {
		this.author_name = author_name;
	}

	public String getAuthor_url() {
		return author_url;
	}

	public void setAuthor_url(String author_url) {
		this.author_url = author_url;
	}

	public String getPost_url() {
		return post_url;
	}

	public void setPost_url(String post_url) {
		this.post_url = post_url;
	}

	public String getPost_time() {
		return post_time;
	}

	public void setPost_time(String post_time) {
		this.post_time = post_time;
	}

	public String getPost_title() {
		return post_title;
	}

	public void setPost_title(String post_title) {
		this.post_title = post_title;
	}

	public List<String> getTags() {
		return tags;
	}

	public void setTags(List<String> tags) {
		this.tags = tags;
	}

	public String getPost_tags() {
		return post_tags;
	}

	public void setPost_tags(String post_tags) {
		this.post_tags = post_tags;
	}

	public String getPost_content() {
		return post_content;
	}

	public void setPost_content(String post_content) {
		this.post_content = post_content;
	}

	public String getSalary() {
		return salary;
	}

	public void setSalary(String salary) {
		this.salary = salary;
	}

	public String getExperience() {
		return experience;
	}

	public void setExperience(String experience) {
		this.experience = experience;
	}

	public String getComp_name() {
		return comp_name;
	}

	public void setComp_name(String comp_name) {
		this.comp_name = comp_name;
	}

	public String getComp_url() {
		return comp_url;
	}

	public void setComp_url(String comp_url) {
		this.comp_url = comp_url;
	}

	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	public String getComp_scale() {
		return comp_scale;
	}

	public void setComp_scale(String comp_scale) {
		this.comp_scale = comp_scale;
	}

	public String getComp_field() {
		return comp_field;
	}

	public void setComp_field(String comp_field) {
		this.comp_field = comp_field;
	}

	public String getFinancing() {
		return financing;
	}

	public void setFinancing(String financing) {
		this.financing = financing;
	}

	public String getComp_vision() {
		return comp_vision;
	}

	public void setComp_vision(String comp_vision) {
		this.comp_vision = comp_vision;
	}

	public String getExtractTime() {
		return extractTime;
	}

	public void setExtractTime(String extractTime) {
		this.extractTime = extractTime;
	}

	public int getHistory() {
		return history;
	}

	public void setHistory(int history) {
		this.history = history;
	}

}
